package com.linsen.core.web.config;

import com.linsen.core.common.annotation.AutoConfiguration;
import com.linsen.core.common.factory.YamlPropertySourceFactory;
import com.linsen.core.web.server.UndertowServerFactoryCustomizer;
import io.undertow.Undertow;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import static io.undertow.UndertowOptions.ENABLE_HTTP2;

/**
 * Undertow http2 h2c 配置，对 servlet 开启
 * @author zhaoyong
 * @date 2023/3/13
 */
@AutoConfiguration
@ConditionalOnClass(Undertow.class)
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:linsen-server.yml")
public class LinsenUndertowHttp2AutoConfiguration {

    @Bean
    public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowHttp2WebServerFactoryCustomizer() {
        return factory -> factory.addBuilderCustomizers(builder -> builder.setServerOption(ENABLE_HTTP2, true));
    }

    /**
     * 实例化UndertowServerFactoryCustomizer，解决undertow启动提示warn的问题
     * @return UndertowServerFactoryCustomizer
     */
    @Bean
    public UndertowServerFactoryCustomizer undertowServerFactoryCustomizer() {
        return new UndertowServerFactoryCustomizer();
    }
}
